﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using NetBay.Core.BusinessEntities;
using Rio.Framework.Localisation;
using Rio.Framework.Pages.Controls;

namespace Rio.Framework.Pages.ChildWindows
{
    public partial class FolderSearchChildWindow
    {
        //Control for result of a folder search
        private SearchPanel m_oFolderSearchPanelControl;
        private ListFolderSearchControl m_oListFolderSearchControl;

        public delegate void LoadSelectedFolderInformationFromListHandler(object sender, NetBay.Core.BusinessEntities.Folder P_oFolder, NetBay.Core.BusinessEntities.FolderType P_oFolderType, NetBay.Core.BusinessEntities.Organization P_oOrganization);
        public event LoadSelectedFolderInformationFromListHandler LoadSelectedFolderInformationFromList;

        /// <summary>
        ///  method for raise the event 'LoadSelectedFolderInformation'
        /// </summary>
        private void OnLoadSelectedFolderInformationFromList(NetBay.Core.BusinessEntities.Folder P_oFolder, NetBay.Core.BusinessEntities.FolderType P_oFolderType, NetBay.Core.BusinessEntities.Organization P_oOrganization)
        {
            if (LoadSelectedFolderInformationFromList != null)
                LoadSelectedFolderInformationFromList(this, P_oFolder, P_oFolderType, P_oOrganization);
        }


        //Control for result of a document search
        private string m_FolderLbl = string.Empty;

        /// <summary>
        /// Initializes a new instance of the <see cref="FolderSearchChildWindow" /> class.
        /// </summary>
        /// <param name="currentUser">The current user.</param>
        public FolderSearchChildWindow(User currentUser)
        {
            InitializeComponent();

            this.KeyUp += MessageBoxChildWindow_KeyUp;

            m_oFolderSearchPanelControl = new SearchPanel();
            m_oFolderSearchPanelControl.IsImportSearch = true;

            m_oListFolderSearchControl = new ListFolderSearchControl();

            //Display an empty table to avoid a full empty panel.
            stackResult.Children.Add(m_oListFolderSearchControl);
            controlSearchPanel.Child = m_oFolderSearchPanelControl;

            InitLabel();

            //Dynamic index created in searchPanel
            m_oFolderSearchPanelControl.ListIndexPropertyChanged += controlSearchPanel_ListIndexPropertyChanged;
            
            //Document and folder search result
            m_oFolderSearchPanelControl.FolderSearchPropertyChanged += controlSearchPanel_FolderSearchPropertyChanged;
            m_oListFolderSearchControl.LoadSelectedFolderInformation += m_oListFolderSearchControl_LoadSelectedFolderInformation;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="FolderSearchChildWindow" /> class.
        /// </summary>
        /// <param name="currentUser">The current user.</param>
        /// <param name="organization">The organization.</param>
        /// <param name="folderType">Type of the folder.</param>
        /// <param name="indexWithValues">The index with values.</param>
        /// <param name="folders">The folders.</param>
        public FolderSearchChildWindow(User currentUser, Organization organization, FolderType folderType, Dictionary<Index, string> indexWithValues, ObservableCollection<Folder> folders)
            : this(currentUser)
        {
            m_oFolderSearchPanelControl.ListIndex = indexWithValues.Keys.ToList();
            m_oFolderSearchPanelControl.m_oCurrentOrganization = organization;
            m_oFolderSearchPanelControl.IndexValues = indexWithValues.Values.ToList();
            m_oFolderSearchPanelControl.m_oCurrentFolderType = folderType;
            m_oFolderSearchPanelControl.FolderSearchResult = folders.ToList();
        }

        /// <summary>
        /// Handles the KeyUp event of the MessageBoxChildWindow control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.KeyEventArgs" /> instance containing the event data.</param>
        private void MessageBoxChildWindow_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                this.DialogResult = false;
                this.Close();
            }
        }

        /// <summary>
        /// M_oes the list folder search control_ load selected folder information.
        /// </summary>
        /// <param name="P_oFolder">The p_o folder.</param>
        /// <param name="P_oFolderType">Type of the p_o folder.</param>
        /// <param name="P_oOrganization">The p_o organization.</param>
        private void m_oListFolderSearchControl_LoadSelectedFolderInformation(Folder P_oFolder, FolderType P_oFolderType, Organization P_oOrganization)
        {
            OnLoadSelectedFolderInformationFromList(P_oFolder, P_oFolderType, P_oOrganization);
            this.DialogResult = true;
            this.Close();
        }

        /// <summary>
        /// Translation
        /// </summary>
        private void InitLabel()
        {
            m_FolderLbl = Resx.GetLabel(ResxSearchPage.FOLDER);
            this.Header = Resx.GetLabel(ResxCommon.SEARCH_FOLDER);
        }

        #region search result
        /// <summary>
        /// FolderType has been selected, so we have index associated
        /// We can build gridview with index in column
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void controlSearchPanel_ListIndexPropertyChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            m_oListFolderSearchControl = new ListFolderSearchControl(e.NewValue as List<Index>);
            m_oListFolderSearchControl.LoadSelectedFolderInformation += m_oListFolderSearchControl_LoadSelectedFolderInformation;   
        }

        /// <summary>
        /// Folder Search has been launched
        /// Enable to bind folder Search result with the gridview
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void controlSearchPanel_FolderSearchPropertyChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            stackResult.Children.Clear();

            m_oListFolderSearchControl.OrganizationSelected = m_oFolderSearchPanelControl.m_oCurrentOrganization;
            m_oListFolderSearchControl.FolderTypeSelected = m_oFolderSearchPanelControl.m_oCurrentFolderType;
            m_oListFolderSearchControl.ListFolders = (List<Folder>)e.NewValue;
            
            stackResult.Children.Add(m_oListFolderSearchControl);
        }

        #endregion
    }
}
